import axios from 'axios'

const baseURL = 'https://www.fastmock.site/mock/07065e4dfb5e94e8751933c9f973d95c/mock'
const timeout = 10000 // 超时时间10秒

/** axios初始化实例 */
const instance = axios.create({
  baseURL: baseURL,
  timeout: timeout
})

/** 封装的axios get请求方法 */
export const get = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    instance
      .get(url, {
        params
      })
      .then(
        res => {
          resolve(res.data)
        },
        err => {
          reject(err)
        }
      )
  })
}

/** 封装的axios post请求方法 */
export const post = (url, data = {}) => {
  return new Promise((resolve, reject) => {
    instance
      .post(url, data, {
        headers: {
          'Content-Tpye': 'application/json'
        }
      })
      .then(
        res => {
          resolve(res.data)
        },
        err => {
          reject(err)
        }
      )
  })
}
